package com.jader.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jader.activities.CollectActivity;
import com.jader.activities.LoginActivity;
import com.jader.activities.MyTendsActivity;
import com.jader.activities.OrderActivity;
import com.jader.activities.SettingActivity;
import com.jader.activities.ShopCartActivity;
import com.jader.activities.UserInfoActivity;
import com.jader.activities.WalletActivity;
import com.jader.beans.User;
import com.jader.util.JaderApplication;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class MineFragment extends Fragment implements OnClickListener {
	private LinearLayout MyColllect, MyShopCart, MyTends, MySetting, MyOder;
	private RelativeLayout UserInfoLayout, NoLoginLayout;
	private View view;
	private Activity activity;
	private Button btn_noLogin;
	private ImageView img_mine_userpic;
	private TextView tv_mint_username, tv_mine_balance;
	private User user;
	String url = JaderApplication.getIP() + "/Jader/client";

	@Override
	public void onAttach(Activity activity) {
		this.activity = activity;
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_mine, null);
		initView();
		return view;
	}

	@Override
	public void onResume() {
		judgeLogin();
		super.onResume();

	}

	/** 判断是否已登录 */
	private void judgeLogin() {
		if ("".equals(PreferenceUtil.getInstance(activity).getShareUserId())
				|| JaderApplication.user == null) {
			UserInfoLayout.setVisibility(View.GONE);
			NoLoginLayout.setVisibility(View.VISIBLE);
		} else {
			String qian = PreferenceUtil.getInstance(activity)
					.getShareUserMoney();
			double money = 0;
			if (!JaderApplication.isEmpty(qian)) {
				money = Double.parseDouble(qian);
			}
			user = new User(Integer.parseInt(PreferenceUtil.getInstance(
					activity).getShareUserId()), PreferenceUtil.getInstance(
					activity).getShareUserPic(), PreferenceUtil.getInstance(
					activity).getShareUserPhone(), PreferenceUtil.getInstance(
					activity).getSharePassWord(), PreferenceUtil.getInstance(
					activity).getShareUserName(), PreferenceUtil.getInstance(
					activity).getShareUserGender(), money);

			if (!JaderApplication.isEmpty(PreferenceUtil.getInstance(activity)
					.getShareUserMoney())) {
				user.setMoney(Double.parseDouble(PreferenceUtil.getInstance(
						activity).getShareUserMoney()));
			}
			RequestParams params = new RequestParams();
			params.addBodyParameter("type", "" + JaderApplication.USER_LOGIN);
			params.addBodyParameter("login_phone", user.getPhone());
			params.addBodyParameter("password", user.getPassword());
			if (JaderApplication.user != null) {

				if (JaderApplication.user.getMoney() == 0) {
					tv_mine_balance.setText("去充值");
				} else {
					tv_mine_balance.setText("￥"
							+ JaderApplication.getPrice(JaderApplication.user
									.getMoney()));
				}
				initData();
			}
			UserInfoLayout.setVisibility(View.VISIBLE);
			NoLoginLayout.setVisibility(View.GONE);
		}
	}

	private void initData() {
		String userPic = user.getPicture();
		String userName = user.getUsername();
		JaderApplication.getBitmapUtils().display(img_mine_userpic,
				JaderApplication.getIP() + "/Jader" + userPic);
		tv_mint_username.setText(userName);

	}

	private void initView() {
		MyColllect = (LinearLayout) view.findViewById(R.id.MyCollect);
		MySetting = (LinearLayout) view.findViewById(R.id.MySetting);
		MyShopCart = (LinearLayout) view.findViewById(R.id.MyShopCart);
		MyTends = (LinearLayout) view.findViewById(R.id.MyTends);
		MyOder = (LinearLayout) view.findViewById(R.id.MyOrder);
		UserInfoLayout = (RelativeLayout) view.findViewById(R.id.MyInfo);
		NoLoginLayout = (RelativeLayout) view.findViewById(R.id.Mine_NoLogin);
		btn_noLogin = (Button) view.findViewById(R.id.btn_mine_nologin);
		img_mine_userpic = (ImageView) view.findViewById(R.id.img_mine_userpic);
		tv_mint_username = (TextView) view.findViewById(R.id.tv_mine_username);
		tv_mine_balance = (TextView) view.findViewById(R.id.tv_mine_balance);
		// 监听单击事件
		MyOder.setOnClickListener(this);
		MyColllect.setOnClickListener(this);
		MySetting.setOnClickListener(this);
		MyTends.setOnClickListener(this);
		MyShopCart.setOnClickListener(this);
		btn_noLogin.setOnClickListener(this);
		img_mine_userpic.setOnClickListener(this);
		tv_mine_balance.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.MySetting:
			Intent MySetting = new Intent(activity, SettingActivity.class);
			startActivity(MySetting);
			break;
		case R.id.btn_mine_nologin:
			Intent login = new Intent(activity, LoginActivity.class);
			startActivity(login);
			break;
		case R.id.tv_mine_balance:
			Intent wallet = new Intent(activity, WalletActivity.class);
			startActivity(wallet);
			break;
		default:
			break;
		}

		switch (v.getId()) {
		case R.id.MyCollect:
			// 跳转到收藏界面
			if (JaderApplication.user != null) {
				Intent Collect = new Intent(activity, CollectActivity.class);
				startActivity(Collect);
			} else {
				JaderApplication.login(activity);
			}
			break;
		case R.id.MyTends:
			if (JaderApplication.user != null) {
				Intent MyTends = new Intent(activity, MyTendsActivity.class);
				startActivity(MyTends);
			} else {
				JaderApplication.login(activity);
			}
			break;

		case R.id.MyShopCart:
			if (JaderApplication.user != null) {
				Intent myShopCat = new Intent(activity, ShopCartActivity.class);
				startActivity(myShopCat);
			} else {
				JaderApplication.login(activity);
			}
			break;
		case R.id.MyOrder:
			if (JaderApplication.user != null) {
				Intent Myorder = new Intent(activity, OrderActivity.class);
				startActivity(Myorder);
			} else {
				JaderApplication.login(activity);
			}
			break;
		case R.id.img_mine_userpic:
			Intent userinfo = new Intent(activity, UserInfoActivity.class);
			startActivity(userinfo);
			break;
		default:
			break;
		}

	}

	protected void loginCheck(RequestParams params) {
		JaderApplication.httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.user = user;
					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						if ("null".equals(result)) {
							JaderApplication.show(activity, "账户已过期，请重新登录");
							PreferenceUtil.getInstance(activity)
									.cleanUserInfo();
							activity.startActivity(new Intent(activity,
									LoginActivity.class));
						} else {
							Gson gson = new Gson();
							User user = gson.fromJson(result, User.class);
							String userid = "" + user.getUserid();
							String username = user.getUsername();
							String password = user.getPassword();
							String user_pic = user.getPicture();
							String phone = user.getPhone();
							String gender = user.getGender();
							Log.e("share", userid + username + password + phone
									+ gender);
							// 缓存用户数据
							PreferenceUtil.getInstance(activity)
									.setShareUserId(userid);
							PreferenceUtil.getInstance(activity)
									.setShareUserName(username);
							PreferenceUtil.getInstance(activity)
									.setSharePassWord(password);
							PreferenceUtil.getInstance(activity)
									.setShareUserPic(user_pic);
							PreferenceUtil.getInstance(activity)
									.setShareUserPhone(phone);
							PreferenceUtil.getInstance(activity)
									.setShareUserGender(gender);
							PreferenceUtil.getInstance(activity)
									.setShareUserMoney(user.getMoney());
							// 显示登录成功，并跳转
							JaderApplication.user = user;
							initData();
						}
					}
				});

	}
}
